<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<%
    String relationBizId = request.getParameter("relationBizId");
    String isDetail = request.getParameter("isDetail");
    String isShowStlDt = request.getParameter("isShowStlDt");
%>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>流程中资金来款操作</title>
    <jsp:include page="../../import.jsp" flush="true"/>
</head>
<body class="iframeBody">
    <form id="stl_form">
        <div class="fieldset1 tabel_col clearFix">
            <div class="fieldBox col_33">
                <div class="label">结清时间</div>
                <div class="field">
                    <input ligerui="width:125" id="stlDt" type="text" value="" class="ui-datepicker" validate="{required:true}">
                    <input id="prjPrdDtlId" name="prjPrdDtlId" value="" type="text" style="display: none;">
                    <span id="saveStlDtBtn" class="sq-button sq-button-round">保存</span>
                </div>
            </div>
        </div>
    </form>
    <div class="fieldset1 tabel_col tabel_col_1 clearFix">
        <div class="caption">
            <div class="btnsBox inLC">
                <span id="fundAddBtn" class="sq-button sq-button-round">选择资金</span><br/>
            </div>
            <span class="title">资金列表</span>
        </div>
        <div class="fieldBox col_100">
            <div id="ligergrid_fundlist" class="sq-grid"></div>
        </div>
    </div>

    <div class="fieldset1 tabel_col tabel_col_1 clearFix">
        <div class="caption">
            <span class="title">保证金列表</span>
        </div>
        <div class="fieldBox col_100">
            <div id="feeOutGrid" class="sq-grid"></div>
        </div>
    </div>

<script>
    var basePath = '<%=path%>';
    var relationBizId = '<%=relationBizId%>';
    var isDetail = '<%=isDetail%>';
    var isShowStlDt = '<%=isShowStlDt%>';
    // 是否允许资金列表、保证金列表、结清时间的操作：true-不允许
    var isShowFundOpt_flag = (isDetail == '' || isDetail == '1');
    var l_hidden = isShowFundOpt_flag ? "l-hidden" : "";
    var fundGrid = null, feeOutGrid = null;
    $(function () {
        console.log("结清时间是否显示isShowStlDt: "+isShowStlDt);
        console.log("资金列表相关参数isDetail: "+isDetail);
        if (isShowStlDt == '0') {
            $("#stl_form").hide();
        }
        initPageData();
        $("#stl_form").ligerForm({
            validate: true
        });
        // 保存结清时间
        $("#saveStlDtBtn").click(function (event) {
            var form = liger.get("stl_form");
            if (!form.valid()) {
                $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_MUST_FIX_ITEM_INFO, SQ_type: "warning"});
                return false; //验证不通过，不执行以下代码
            }
            var data = form.getData();
            data['stlDt']=$("#stlDt").val();
            $.ligerDialog.hide();

            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: data,
                url: basePath + '/capital/capFund/modifyStlDt.do',
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        });

        $("#fundAddBtn").click(function (event) {
            $.ligerDialog.SQopen({
                width: 950,
                height: 490,
                title: '选择资金列表',
                url: basePath + '/capital/capFund/flow/toChooseFund.do?type=1&bizTypCd=1&relationPayId=' + relationBizId,
                fixButtons: true,
                allowClose: false
            });
        });

        fundGrid = $("#ligergrid_fundlist").ligerGrid({
            url: basePath + '/capital/capFund/initFundList.do',
            parms: {'relationPayId': relationBizId},
            method: 'get',
            columns: [
                {display: '资金编号', name: 'code', width: 'auto', align: 'center', enabledSort: false,
                    render: function (rowdata, index, value) {
                        return '<a href="javascript:;" style="cursor: pointer;" onclick="qyFund(' + rowdata.id + ')">' + rowdata.code + '</a>';
                    }
                },
                {display: '收款账户', name: 'acceptName', width: 'auto', align: 'center', enabledSort: false},
                {display: '交易(到账)日期', name: 'acceptDate', width: 'auto', align: 'center', enabledSort: false,render:renderDate},
                {display: '来款账号', name: 'comeCode', width: 'auto', align: 'center', enabledSort: false},
                {display: '来款户名', name: 'comeName', width: 'auto', align: 'center', enabledSort: false},
                {display: '来款金额', name: 'amount', width: 'auto', align: 'right', enabledSort: false,
                    render:function(rowdata,index,value){
                        return '&yen;'+FormatMoney(rowdata.amount/100);
                    }
                },
                {display: '可用金额', name: 'availableAmount', width: 'auto', align: 'right', enabledSort: false,
                    render:function(rowdata,index,value){
                        return '&yen;'+FormatMoney(rowdata.availableAmount/100);
                    },
                    totalSummary:{
                        render: function (rowdata, column, cell){
                            if (rowdata.sum) {
                                return '<div style="color:red">合计：' + '&yen;' + FormatMoney(rowdata.sum / 100) + '</div>';
                            } else {
                                return '<div style="color:red">合计：' + '&yen;0.00</div>';
                            }
                        },
                        align: 'left'
                    },
                    editor: {
                        type: 'money', precision: 2,
                        toFix: false,
                        onToFix: function () {
                            top.$.ligerDialog.SQalert({SQ_type: "warning", content: "小数点后面最多2位小数！"});
                        }
                    }

                },
                {display: '客户名称', name: 'cstNm', width: 'auto', align: 'center', enabledSort: false,},
                {display: '操作', width: 200, align: 'center', enabledSort: false,
                    render: function (rowdata, index, value) {
                        var opt="";
                        opt +="<a class='sq-button-hasicon " + l_hidden + "' href='javascript:;' onclick='delFund(" + rowdata.id + ")'><i class='sqBtnIco_7'></i>删除</a>";
                        return opt;
                    }
                }
            ],
            usePager: true,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: '240',
            heightDiff: -2,
            checkbox: false,
            rownumbers: true,
            enabledEdit: !isShowFundOpt_flag,
            allowUnSelectRow: true,
            alternatingRow: true,
            onAfterEdit: function (e) {
                if (e.column.name == "availableAmount") {
                    var row = fundGrid.getRow(e.rowindex);
                    var amount = row.amount;
                    var newVal = e.value;
                    if (parseInt(newVal) > parseInt(amount)) {
                        fundGrid.reload();
                        $.ligerDialog.SQalert({content: "修改后的可用金额不能大于该资金的来款金额！", SQ_type: "error"});
                        return;
                    }
                    $.ligerDialog.confirm("是否确认修改该笔资金的可用金额?", function (flag) {
                        if (flag == false) {
                            fundGrid.reload();
                            return;
                        }
                        var wait_dialog;
                        $.ajax({
                            type: 'post',
                            dataType: 'json',
                            data: {id: row.id, availAmt: parseFloat(newVal/100)},
                            url: basePath+'/capital/capFund/flow/modifyChildFund.do',
                            beforeSend: function () {
                                wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
                            },
                            success: function (msg) {
                                wait_dialog.close();
                                if (msg.success == 1) {
                                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                                } else {
                                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                                }
                                fundGrid.reload();
                            }
                        });
                    });
                }
            }
        });

        // 初始化保证金列表
        feeOutGrid = $("#feeOutGrid").ligerGrid({
            url: basePath + '/capital/capFeeOutLog/initList.do?qtype=0&paySchId=' + relationBizId,
            method: 'get',
            columns: [
                {display: '支付表号', name: 'paySchNo', minWidth: 100, align: 'center', enabledSort: false},
                {display: '费用类型', name: 'feeTypCdNm', minWidth: 100, align: 'center', enabledSort: false},
                {display: '费用金额', name: 'feeAmt', minWidth: 100, align: 'right', enabledSort: false,
                    render: function (rowdata, index, value) {
                        return '&yen;' + FormatMoney(rowdata.feeAmt / 100);
                    }
                },
                {
                    display: '转出金额', name: 'outAmt', minWidth: 100, align: 'right', enabledSort: false,
                    render: function (rowdata, index, value) {
                        return '&yen;' + FormatMoney(rowdata.outAmt / 100);
                    }
                },
                {display: '操作', name: '', minWidth: 100, align: 'left', enabledSort: false,
                    render:function(rowdata,index,value) {
                        if (rowdata.feeAmt > rowdata.outAmt) {
                            var opt = "";
                            opt += "<a class='sq-button-hasicon " + l_hidden + "' href='javascript:;' onclick='feeOut(\" + index + \")'><i class='sqBtnIco_17'></i>转出</a>";
                            return opt;
                        }
                    }
                },
            ],
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: '240',
            heightDiff: -2,
            checkbox: false,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: false
        });
    });

    function initPageData() {
        if (isShowFundOpt_flag) {
            $("#stlDt").ligerDateEditor().setDisabled();
            $("#saveStlDtBtn").hide();
            $("#fundAddBtn").hide();
        }

        var wait_dialog;
        $.ajax({
            type: 'post',
            dataType: 'json',
            data: {paySchId: relationBizId},
            url: basePath+'/capital/capFund/flow/initPageFlowData.do',
            beforeSend: function () {
                wait_dialog = $.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING, SQ_type: "waitting"});
            },
            success: function (msg) {
                wait_dialog.close();
                if (msg.success == 1) {
                    $("#stlDt").val(msg.data.stlDt);
                    $("#prjPrdDtlId").val(msg.data.prjPrdDtlId);
                    //top.$.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                } else {
                    $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                }
            }
        });
    }

    //废除
    function delFund(id) {
        $.ligerDialog.confirm("是否确认删除该笔资金?", function (flag) {
            if (flag == false) return;
            var wait_dialog;
            $.ajax({
                type: 'post',
                dataType: 'json',
                data: {id: id},
                url: basePath + '/capital/capFund/flow/delChildFund.do',
                beforeSend: function () {
                    wait_dialog = $.ligerDialog.SQalert({
                        content: LogUtils.LOG_CMM_DATA_LOADING,
                        SQ_type: "waitting"
                    });
                },
                success: function (msg) {
                    wait_dialog.close();
                    if (msg.success == 1) {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "success"});
                        fundGrid.reload();
                    } else {
                        $.ligerDialog.SQalert({content: msg.info, SQ_type: "error"});
                    }
                }
            });
        });
    }

    function feeOut(index){
        var data = feeOutGrid.getRow(index);
        //var amt=data.feeAmt-data.outAmt;
        var amt = NumberUtils.sub(data.feeAmt, data.outAmt);
        var params = "?paySchId=" + data.paySchId
            + "&paySchNo=" + data.paySchNo
            + "&paySchDId=" + data.paySchDId
            + "&feeTypCd=" + data.feeTypCd
            + "&feeAmt=" + data.feeAmt
            + "&outAmt=" + amt
            + "&relationId=" + data.paySchId;
        $.ligerDialog.open({
            width: 550,
            height: 400,
            title: '保证金转出',
            url: basePath + '/capital/capFeeOutLog/toAddPage.do' + params,
            onClose: function () {
                top.$.ligerDialog.SQalert({content: "保证金转出成功！", SQ_type: "success"});
                feeOutGrid.reload();
            }
        });
    }

    //查询资金信息
    function qyFund(_id) {
        var onCancel = function (item, Dialog) {
            Dialog.hide();
        };
        $.ligerDialog.SQopen({
            width: 800,
            //height: 300,
            autoFixHeight: true,
            title: '资金信息',
            url: basePath + "/capital/capFund/toFundDtlPage.do?id=" + _id,
            buttons: [{
                text: "关闭", onclick: onCancel, type: 'cancel'
            }
            ]
        });
    }

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DD);
        }
        return dateStr;
    }
</script>
</body>
</html>
